<template>
  <Pannel ref="oPannel" @refresh="onRefresh" @load="onLoad" :finshed="finshed">
    <div class="contonier" v-if="show">
      <div class="swiper">
        <img :style="{ width: '100%' }" src="@/assets/images/EconomicInstitutions2.png" alt="" />
      </div>
      <div class="tabs">
        <PopupRight class="popup-right">
          <DSelect v-model="filters.area" readonly clickable label="位置" placeholder="请选择位置"></DSelect>
          <DSelect v-model="filters.state" readonly clickable label="状态" placeholder="请选择状态"></DSelect>
          <DSelect v-model="filters.kind" readonly clickable label="类型" placeholder="请选择类型"></DSelect>
        </PopupRight>
        <div class="search">
          <van-search v-model="search" show-action placeholder="请输入搜索关键词" @search="onSearch">
            <template #action>
              <div @click="onSearch">搜索</div>
            </template>
          </van-search>
        </div>
      </div>
      <div class="list">
        <ListItem class="item" v-for="(item, index) of list" :key="index" :item="item"></ListItem>
      </div>
    </div>
  </Pannel>
</template>

<script>
import { mapState, mapActions, mapGetters } from 'vuex'

import PopupRight from '../components/PopupRight'
import ListItem from '../components/ListItem'
import { list } from './data'

export default {
  name: 'EconomicInstitutions',
  components: { ListItem, PopupRight },
  data() {
    return {
      show: true,
      curIndex: 0,
      finshed: false,
      search: '',
      list: [],
      total: 0,
      filters: {
        area: '',
        state: '',
        kind: ''
      },
      page: {
        pageNum: 1,
        pageSize: 10
      },
      btnList: [{ text: '经纪机构查询' }, { text: '从业人员查询' }]
    }
  },
  computed: {
    ...mapState({
      dists: state => {
        return state
      }
    })
  },
  created() {
    this.total = list.length
  },
  mounted() {
    this.setDicts(['area'])
    this.setDicts(['state'])
    this.setDicts(['kind'])
  },
  methods: {
    ...mapActions(['setDicts']),
    onSearch() {},
    onRefresh() {},
    onLoad(func) {
      //  传入一个回调函数
      func(fetchDataList.bind(this))
      //  加载页面
    },
    handleChangeBtn(index) {
      this.curIndex = index
    },
    handleIsActive(index) {
      if (this.curIndex === index) {
        return '#fff'
      } else {
        return 'transparent'
      }
    }
  }
}

async function fetchDataList() {
  if (this.list.length >= this.total) return (this.finshed = true)
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      const startIndex = (this.page.pageNum - 1) * this.page.pageSize
      const endIndex = this.page.pageNum * this.page.pageSize
      this.list = this.list.concat(list.slice(startIndex, endIndex))
      resolve()
      this.$refs.oPannel.onError('加载失败了');
      this.page.pageNum++
    }, 1000)
  })
}
</script>
<style lang="scss" scoped>
:deep .van-search {
  background: none;
}

.contonier {
  position: relative;
  padding: 0 24px;

  .list {
    margin-top: 32px;

    .item {
      margin-bottom: 24px;
    }
  }

  .tabs {
    display: flex;
    font-size: 32px;
    align-items: center;
    background-color: #fff;
    padding: 0 20px;

    .popup-right {
      width: 80px;
    }

    .search {
      flex: 1;
    }
  }
}
</style>